﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Brc_PaaS.Common.Model.Basic;
using Brc_PaaS.Common.Model.RabbitMq;
using Brc_PaaS.Core.RabbitMq;
using Exceptionless;
using Exceptionless.Logging;

namespace Brc_PaaS.Bll.RabbitMq
{
    public class RabbitMqManager : IRabbitMqManager
    {
        private IRabbitMqClient _client;
        public RabbitMqManager(IRabbitMqClient client)
        {
            this._client = client;
        }
        public BaseResponseModel SendMessage(RabbitMQSettingEntity entity, EventMessage eventMessage, string queueName, string systemId, string systemName)
        {
            var result = _client.SendMessage(entity, eventMessage, queueName);
            if (!result.IsSuccess)
            {
                Task.Run(() =>
                {
                    ExceptionlessClient.Default.CreateLog("Brc_PaaS.Bll.RabbitMq.RabbitMqManager.SendMessage", $"业务系统：{systemName}(系统Id为【{systemId}】)，于【{DateTime.Now.ToString("s")}】发送消息【{eventMessage.Message}】到消息队列（虚拟机名称：{entity.VirtualHost},交换机：{entity.ExchangeName}）失败。失败原因：【{result.Msg}】", LogLevel.Error).Submit();
                });
            }
            return result;
        }
    }
}
